package com.jswlw.service.biz.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jswlw.common.utils.CountUtils;
import com.jswlw.common.utils.StringUtils;
import com.jswlw.dao.base.UserMapper;
import com.jswlw.dao.biz.DistributorMapper;
import com.jswlw.entity.base.User;
import com.jswlw.entity.biz.DevType;
import com.jswlw.entity.biz.Distributor;
import com.jswlw.service.base.UserService;
import com.jswlw.service.biz.DevTypeService;
import com.jswlw.service.biz.DistributorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DistributorServiceImpl implements DistributorService {

    @Autowired
    DistributorMapper mapper;

    @Autowired
    UserService userService;

    @Autowired
    DevTypeService devTypeService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Distributor distributor) {
        //设置ID
        if (distributor.getParentId().equals(1000)) {
            distributor.setLevel(2);
            distributor.setId(CountUtils.toNext(1, queryMaxCount(1000)));
        } else {
            distributor.setLevel(String.valueOf(distributor.getParentId()).length() / 3 + 2);
            distributor.setId(CountUtils.toNext(distributor.getParentId(), queryMaxCount(distributor.getParentId())));
        }
        distributor.setLl(0);
        distributor.setSc(0);
        mapper.insertSelective(distributor);
        //新增用户信息
        User user = new User();
        user.setLoginName(String.valueOf(distributor.getId()));
        user.setPassword(distributor.getPassword());
        user.setNickName(distributor.getName());
        user.setUserType(2);
        user.setStatus(1);
        user.setRoleId(2);
        userService.insert(user);
    }

    @Override
    public void update(Distributor distributor) {
        if (Objects.nonNull(distributor.getPassword())) {
            User user = userService.find4LoginName(String.valueOf(distributor.getId()));
            user.setPassword(distributor.getPassword());
            userService.update(user);
        }
        mapper.updateByPrimaryKeySelective(distributor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        mapper.deleteByPrimaryKey(id);
        userService.delete4LoginName(String.valueOf(id));
    }

    @Override
    public Distributor find4Id(Integer id) {
        Distributor distributor = mapper.selectByPrimaryKey(id);
        List<DevType> devTypeList = new ArrayList<>();
        List<Integer> devTypeIdList = new ArrayList<>();
        if (StringUtils.isNotEmpty(distributor.getDevTypes())) {
            List<Integer> devTypeIds = JSON.parseArray(distributor.getDevTypes(), Integer.class);
            if (Objects.nonNull(devTypeIds)) {
                devTypeIds.forEach(devType -> {
                    devTypeList.add(devTypeService.find4Id(devType));
                    devTypeIdList.add(devType);
                });
            }

        }
        distributor.setDevTypeList(devTypeList);
        distributor.setDevTypeIdList(devTypeIdList);
        return distributor;
    }

    @Override
    public List<Distributor> queryList(Distributor filter) {
        return mapper.queryList(filter);
    }

    @Override
    public Page<Distributor> queryPage(Distributor distributor, Integer offset, Integer limit) {
        PageHelper.startPage(offset, limit);
        return (Page<Distributor>) mapper.queryList(distributor);
    }

    @Override
    public int queryMaxCount(Integer parentId) {
        return mapper.queryMaxCount(parentId) + 1;
    }

    @Override
    public List<Integer> getChildrenIds(Integer parentId) {
        List<Integer> list = new ArrayList<>();
        list.add(parentId);
        Distributor filter = new Distributor();
        filter.setParentId(parentId);
        List<Distributor> distributorList = mapper.queryList(filter);
        distributorList.forEach(distributor -> {
            if (distributor.getLevel() == 2) {
                list.addAll(getChildrenIds(distributor.getId()));
            }
            list.add(distributor.getId());
        });
        return list;
    }
}
